package com.store.app.widget;


import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.store.app.activity.WebViewLLMActivity;

@SuppressWarnings("deprecation")
public class ProgressWebView extends WebView {

	private ProgressBar progressbar;
    private Context context;
	private WebViewLLMActivity.onScrollList onScrollList;
	public void setLister(WebViewLLMActivity.onScrollList onScrollList){
		this.onScrollList = onScrollList;
	}
	public ProgressWebView(Context context, AttributeSet attrs) {
		super(context, attrs);
//
//		progressbar = new ProgressBar(context, null,
//				android.R.attr.progressBarStyleHorizontal);
//		progressbar.setProgressDrawable(this.getResources().getDrawable(R.drawable.progressbar));
//		progressbar.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
//				3, 0, 0));
//		addView(progressbar);
//		setWebViewClient(new WebViewClient(){});
//		setWebChromeClient(new WebChromeClient());
	}
	public void setContext(Context c){
		context=c;
	}
   public class WebViewClient extends  android.webkit.WebViewClient{            
	    @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)  
        {                 // Handle the error
  	        Toast.makeText(context, "请检查本地网络！", Toast.LENGTH_SHORT).show();
  	    	view.loadUrl("");
        }        

		@Override
         public boolean shouldOverrideUrlLoading(WebView view, String url)
          {    
			 if(url.equals("http://sgadmin.hithinktank.com/plus/api/www.baidu.com")){
    			  return true;
    			  
    		  }else{
               view.loadUrl(url);                     
               return true;
              }      
          }        	  

		@Override
      public void onPageFinished(WebView view, String url) {  
     }
       
     }
	public class WebChromeClient extends android.webkit.WebChromeClient {
		@Override
		public void onProgressChanged(WebView view, int newProgress) {
			if (newProgress == 100) {
				progressbar.setVisibility(GONE);
			} else {
				if (progressbar.getVisibility() == GONE)
					progressbar.setVisibility(VISIBLE);
				progressbar.setProgress(newProgress);
			}
			super.onProgressChanged(view, newProgress);
		}
		
	}

//	@Override
//	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
//		LayoutParams lp = (LayoutParams) progressbar.getLayoutParams();
//		lp.x = l;
//		lp.y = t;
//		progressbar.setLayoutParams(lp);
//		super.onScrollChanged(l, t, oldl, oldt);
//	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()){
			case MotionEvent.ACTION_DOWN:
				if(this.getScrollY() <= 0)
					this.scrollTo(0,1);
				break;
			case MotionEvent.ACTION_UP:
//                if(this.getScrollY() == 0)
//                this.scrollTo(0,-1);
				break;
		}
		return super.onTouchEvent(event);
	}
	@Override
	protected void onScrollChanged(int l, int t, int oldl, int oldt) {
		super.onScrollChanged(l, t, oldl, oldt);
//		Log.v("zyl","getScrollY():"+getScrollY());
//		if (getScrollY() == 0) {
//			if(onScrollList!=null){
//				onScrollList.onScrollChate(true);
//			}
//		} else {
//			if(onScrollList!=null){
//				onScrollList.onScrollChate(false);
//			}
//		}
	}


}